package com.youjiang.service.impl;

import com.youjiang.dao.ExaminationMapper;
import com.youjiang.dao.ResultMapper;
import com.youjiang.dao.entity.Examination;
import com.youjiang.dao.entity.ExaminationExample;
import com.youjiang.dao.entity.Result;
import com.youjiang.dao.entity.ResultExample;
import com.youjiang.model.Question;
import com.youjiang.model.Score;
import com.youjiang.model.WrongResult;
import com.youjiang.service.IResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yujia on 2017/11/29.
 */
@Service
public class ResultService implements IResultService {
    @Autowired
    private ResultMapper resultMapper;
    @Autowired
    private ExaminationMapper examinationMapper;

    public int setUserResult(Result result) {
        return resultMapper.insert(result);
    }

    public int delUserResultByClassify(Result result) {
        ResultExample resultExample = new ResultExample();
        resultExample.or().andClassifyEqualTo(result.getClassify());
        return resultMapper.deleteByExample(resultExample);
    }

    public Score computeScore(Result result) {
        Score score = new Score();
        List<Result> results = new ArrayList<Result>();
//        总题数
        ResultExample resultExample1 = new ResultExample();
        ResultExample.Criteria criteria1 = resultExample1.createCriteria();
        criteria1.andClassifyEqualTo(result.getClassify());
        criteria1.andUserIdEqualTo(result.getUserId());
        results = resultMapper.selectByExample(resultExample1);
        score.setTotolQuestion(results.size());
//        正确题数
        ResultExample resultExample2 = new ResultExample();
        ResultExample.Criteria criteria2 = resultExample2.createCriteria();
        criteria2.andClassifyEqualTo(result.getClassify());
        criteria2.andUserIdEqualTo(result.getUserId());
        criteria2.andIsRightEqualTo(1);
        results = resultMapper.selectByExample(resultExample2);
        score.setRightQuestion(results.size());
//        错误题目
        ResultExample resultExample3 = new ResultExample();
        ResultExample.Criteria criteria3 = resultExample3.createCriteria();
        criteria3.andClassifyEqualTo(result.getClassify());
        criteria3.andUserIdEqualTo(result.getUserId());
        criteria3.andIsRightEqualTo(0);
        results = resultMapper.selectByExample(resultExample3);
        score.setWrongResults(transferResultToWrong(results));

        return score;
    }

    public List<WrongResult> transferResultToWrong(List<Result> results){
        List<WrongResult> wrongResults = new ArrayList<WrongResult>();
        for (Result result:results) {
            WrongResult wrongResult = new WrongResult();
            Examination exam = examinationMapper.selectByPrimaryKey(result.getQuestionId());
            wrongResult.setQuestionId(result.getQuestionId());
            wrongResult.setTitle(exam.getTitle());
            wrongResult.setUserAnswer(result.getUserAnswer());
            wrongResult.setIsRight(result.getIsRight());
            String right = "";
            switch(exam.getAnswer().toCharArray()[0]){
                case 'A':right = exam.getAnswera();break;
                case 'B':right = exam.getAnswerb();break;
                case 'C':right = exam.getAnswerc();break;
                case 'D':right = exam.getAnswerd();break;
                case '1':right = exam.getAnswer();break;
                case '0':right = exam.getAnswer();break;
            }
            wrongResult.setRightAnswer(right);
            wrongResults.add(wrongResult);
        }
        return  wrongResults;
    }
}
